package com.example.springbootServiceNetwork.demos.web.Controller;

import com.alibaba.fastjson.JSON;
import com.example.springbootServiceNetwork.demos.web.Config.JwtInfo;
import com.example.springbootServiceNetwork.demos.web.Config.Result;
import com.example.springbootServiceNetwork.demos.web.Dto.PageDto;
import com.example.springbootServiceNetwork.demos.web.Entity.UserPurview;
import com.example.springbootServiceNetwork.demos.web.Service.JwtRedistService;
import com.example.springbootServiceNetwork.demos.web.Service.UserPurviewService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import static com.example.springbootServiceNetwork.demos.web.Tool.EncryptUtil.encrypt;

@RestController
@RequestMapping("/purview")
public class UserPurviewController {
    @Resource
    private JwtRedistService jwtRedistService;
    @Resource
    private UserPurviewService userPurviewService;

    /**
     * 查询用户权限列表
     */
    @PostMapping("/queryPurview")
    public String queryUserPurview(@RequestBody PageDto pageDto, @RequestHeader("token") String token){
        JwtInfo jwtInfo =jwtRedistService.getUserInfo(token);
        if(jwtInfo.getPass()){
            return encrypt(JSON.toJSONString(userPurviewService.qureyUserPurview(pageDto,jwtInfo)));
        }else {
            return encrypt(JSON.toJSONString(jwtInfo.getResult())) ;
        }
    }

    /**
     * 修改用户权限
     */
    @PostMapping("/updatePurview")
    public Result updateUserPurview(@RequestBody UserPurview purview, @RequestHeader("token") String token){
        JwtInfo jwtInfo =jwtRedistService.getUserInfo(token);
        if(jwtInfo.getPass()){
            return userPurviewService.updateUserPurview(purview,jwtInfo);
        }else {
            return jwtInfo.getResult() ;
        }
    }

}
